﻿using System;
using System.Collections.Generic;
using USU.MetricsMonitor.Common;

namespace USU.MetricsMonitor.LogParser
{
    /// <summary>
    /// Информация о ревизии
    /// </summary>
    public class RevisionInfo : IEquatable<RevisionInfo>
    {
        /// <summary>
        /// Номер ревизии
        /// </summary>
        public Revision Revision
        {
            get;
            private set;
        }

        /// <summary>
        /// Автор ревизии
        /// </summary>
        public string Author
        {
            get;
            private set;
        }

        /// <summary>
        /// Дата и время ревизии
        /// </summary>
        public DateTime DateTime
        {
            get;
            private set;
        }

        /// <summary>
        /// Коллекция информации о действиях
        /// </summary>
        public IEnumerable<ActionInfo> ActionsInfo
        {
            get;
            private set;
        }

        /// <summary>
        /// Создание информации об одной ревизии
        /// </summary>
        /// <param name="revision">номер ревизии</param>
        /// <param name="author">автор ревизии</param>
        /// <param name="dateTime">дата и время ревизии</param>
        /// <param name="actionsInfo">коллекция информации о действиях</param>
        public RevisionInfo(Revision revision, string author, DateTime dateTime, IEnumerable<ActionInfo> actionsInfo)
        {
            Revision = revision;
            Author = author;
            DateTime = dateTime;
            ActionsInfo = actionsInfo;
        }

        /// <summary>
        /// Метод должен использоваться только в тестовых целях!!!
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string stringRevisionInfo = String.Format("{0} | {1} | {2}\n", Revision, Author, DateTime);
            if (ActionsInfo != null)
            {
                foreach (ActionInfo actionInfo in ActionsInfo)
                {
                    stringRevisionInfo += String.Format(" {0}\n", actionInfo);
                }
            }
            return stringRevisionInfo;
        }

        /// <summary>
        /// Метод должен использоваться только в тестовых целях!!!
        /// </summary>
        /// <param name="otherRevisionInfo"></param>
        /// <returns></returns>
        public bool Equals(RevisionInfo otherRevisionInfo)
        {
            if (
                (Revision != otherRevisionInfo.Revision) ||
                (Author.CompareTo(otherRevisionInfo.Author) != 0) ||
                (DateTime.CompareTo(otherRevisionInfo.DateTime) != 0)
                )
            {
                return false;
            }
            List<ActionInfo> listActionsInfo = new List<ActionInfo>(ActionsInfo);
            List<ActionInfo> listOtherActionsInfo = new List<ActionInfo>(otherRevisionInfo.ActionsInfo);
            foreach (ActionInfo actionInfo in ActionsInfo)
            {
                listOtherActionsInfo.Remove(actionInfo);
            }
            foreach (ActionInfo actionInfo in otherRevisionInfo.ActionsInfo)
            {
                listActionsInfo.Remove(actionInfo);
            }
            return  (
                        (listActionsInfo.Count == 0) &&
                        (listOtherActionsInfo.Count == 0)
                    );
        }
    }
}